package com.meitianhui.convenitenbuy.module.mine.presenter;

import com.meitianhui.convenitenbuy.App;
import com.meitianhui.convenitenbuy.Constants;
import com.meitianhui.convenitenbuy.base.BaseResponse;
import com.meitianhui.convenitenbuy.config.InfoProvider;
import com.meitianhui.convenitenbuy.http.ResponseObserver;
import com.meitianhui.convenitenbuy.module.mine.contract.SetPayPasswordContract;
import com.meitianhui.convenitenbuy.module.mine.http.MineApiDataSource;
import com.meitianhui.convenitenbuy.module.mine.model.UserPaySmsVerifyResponse;
import com.meitianhui.convenitenbuy.module.mine.ui.activity.SetPayPasswordActivity;
import com.meitianhui.convenitenbuy.module.order.model.bean.EmptyInfo;
import com.meitianhui.convenitenbuy.util.MD5Utils;
import com.meitianhui.convenitenbuy.util.TipUtils;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * Created by amy on 2017/11/10.
 */

public class SetPayPasswordPresenter implements SetPayPasswordContract.Presenter {

    private SetPayPasswordContract.View mView;
    private SetPayPasswordActivity mContext;

    public SetPayPasswordPresenter(SetPayPasswordActivity activity,
                                   SetPayPasswordActivity context) {
        this.mContext = context;
        this.mView = activity;
    }

    @Override
    public void attachView(Object view) {
    }

    @Override
    public void detachView() {
    }

    /**
     * 获取短信验证码
     */
    @Override
    public void verifyPhone(String userPhoneNumber) {
        MineApiDataSource.getInstance()
                .getSmsCode(InfoProvider.getAppToken(App.getInstance()),
                        "infrastructure.securityVerify")
                .subscribe(new ResponseObserver<UserPaySmsVerifyResponse>(App.getInstance()) {
                    @Override
                    public void onSuccess(UserPaySmsVerifyResponse o) {
                        // 去获取短信验证
                        MineApiDataSource.getInstance()
                                .getPhoneVerifyCode(InfoProvider.getAppToken(App.getInstance()),
                                        userPhoneNumber, o.data)
                                .subscribe(new ResponseObserver<BaseResponse<EmptyInfo>>(App.getInstance()) {
                                    @Override
                                    public void onSuccess(BaseResponse<EmptyInfo> o) {
                                        TipUtils.showToast(mContext, "获取验证码成功");
                                    }

                                    @Override
                                    public void onError(Throwable e) {
                                        super.onError(e);
                                        TipUtils.showToast(mContext, "获取验证码失败");
                                    }
                                });
                    }

                    @Override
                    public void onError(Throwable e) {
                        super.onError(e);
                        TipUtils.showToast(mContext, "获取验证码失败");
                    }
                });
    }

    @Override
    public Boolean isAllNumber(String mEtPassword1) {
        Pattern p = Pattern.compile("^\\d{6}$");
        Matcher m = p.matcher(mEtPassword1);
        return m.matches();
    }

    /**
     * 提交用户密码
     */
    @Override
    public void submitPassword(String mEtPassword1, String smsVerifyCode, String userPhoneNumber) {
        String passwordSign = MD5Utils.MD5Encode(mEtPassword1, "");
        MineApiDataSource.getInstance()
                .submitPasswordSetting(InfoProvider.getUserToken(mContext).getUser_token(),
                        passwordSign, userPhoneNumber, smsVerifyCode)
                .subscribe(new ResponseObserver<EmptyInfo>(App.getInstance()) {
                    @Override
                    public void onSuccess(EmptyInfo o) {
                        InfoProvider.getUserToken(mContext)
                                .setPayment_password_flag(Constants.PASSWORD_SELECT);
                        mView.setPasswordSucc();
                    }

                    @Override
                    public void onFail(int code, String msg) {
                        super.onFail(code, msg);
                    }
                });
    }

}
